package browserUI
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	
	import flashx.textLayout.debug.assert;
	
	[Event(name="keyPress",type="browserUI.PopUpKeyboardEvent")]
	
	public class PopUpKeyboard extends Sprite
	{
		
		private static var layout1:Vector.<Vector.<String>>=new <Vector.<String>>[
			new <String>["Q","W","E","R","T","Y","U","I","O","P",Key.BACK],
			new <String>["A","S","D","F","G","H","J","K","L",Key.ENTER],
			new <String>[Key.SHIFT,"Z","X","C","V","B","N","M",":",".",Key.SHIFT],
			new <String>["?123","\\",Key.SPACE,"/","?123",Key.CLOSE]
		];
		
		private static var layout2:Vector.<Vector.<String>>=new <Vector.<String>>[
			new <String>["1","2","3","4","5","6","7","8","9","0",Key.BACK],
			new <String>["-","_","!","\"","'","@","<",">","^",Key.ENTER],
			new <String>["&","(",")","+","=","#","{","}","[","]",","],
			new <String>["ABC","%",Key.SPACE,"?",";","ABC",Key.CLOSE]
		];
		
		private var _keys:Vector.<Vector.<Vector.<Key>>>=new Vector.<Vector.<Vector.<Key>>>();
		private var _layouts:Vector.<Sprite>=new Vector.<Sprite>();
		
		private var _nx:int=0;
		
		private var _mask:Shape;
		private var _content:Sprite;
		
		public var open:Boolean;
		private var _openBtn:Key;
		private var _shiftEnabled:Boolean;
		private var _shiftKeys:Vector.<Key>;
		private var _currentLayout:int;
		
		public function PopUpKeyboard()
		{
			super();
			
			mouseEnabled=false;
			
			_mask=new Shape();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0,0,_width,_height);
			addChild(_mask);
			_content=new Sprite();
			_content.mask=_mask;
			_content.y=_height+60;
			_shiftKeys=new Vector.<Key>();
			var layouts:Vector.<Vector.<Vector.<String>>>=new <Vector.<Vector.<String>>>[layout1,layout2];
			for (var l:int=0;l<layouts.length; l++){
				_layouts.push(new Sprite());
				_layouts[l].visible=false;
				_content.addChild(_layouts[l]);
				_keys.push(new Vector.<Vector.<Key>>());
				for (var j:int=0; j<layouts[l].length; j++){
					_keys[l].push(new Vector.<Key>());
					_nx=Math.max(_nx,layouts[l][j].length);
					for (var i:int=0; i<layouts[l][j].length; i++){
						var key:Key=new Key(layouts[l][j][i]);
						_keys[l][j].push(key);
						_layouts[l].addChild(key);
						key.addEventListener(MouseEvent.CLICK,handleKeyPress);
						if(key.id==Key.SHIFT)
							_shiftKeys.push(key);
					}
				}
			}
			_setKeyLayout(0);
			_openBtn=new Key(Key.OPEN);
			_openBtn.width=60;
			_openBtn.height=55;
			_openBtn.y=-57.5;
			_openBtn.addEventListener(MouseEvent.MOUSE_DOWN,handleKeyPress);
			_content.addChild(_openBtn);
			
			addChild(_content);
			redraw();
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function _setKeyLayout(layoutID:int):void{
			_layouts[_currentLayout].visible=false;
			_currentLayout=layoutID;
			_layouts[_currentLayout].visible=true;
		}

		
		private function handleKeyPress(event:MouseEvent):void{
			var id:String=Key(event.currentTarget).id;
			var keyEvt:PopUpKeyboardEvent
			switch(id){
				case Key.CLOSE:
					open=false;
					break;
				case Key.OPEN:
					open=true;
					break;
				case Key.SHIFT:
					_shiftEnabled=!_shiftEnabled;
					Key(event.target).toggleState=_shiftEnabled;
					for each(var k:Key in _shiftKeys)
					k.toggleState=_shiftEnabled;
					break;
				case Key.SPACE:
					keyEvt=new PopUpKeyboardEvent(PopUpKeyboardEvent.KEY_PRESS," ");
					dispatchEvent(keyEvt);
					break;
				case Key.LETTER_MODE:
					_setKeyLayout(0);
					break;
				case Key.NUMBER_MODE:
					_setKeyLayout(1);
					break;
				default:
					keyEvt=new PopUpKeyboardEvent(PopUpKeyboardEvent.KEY_PRESS,_shiftEnabled?id.toUpperCase():id.toLowerCase());
					dispatchEvent(keyEvt);
			}
		}
		
		private var _width:Number=800;
		
		public override function get width():Number
		{
			return _width;
		}
		
		public override function set width(value:Number):void
		{
			_width = value;
		}
		
		private var _height:Number=400;
		
		public override function get height():Number
		{
			return _height;
		}
		
		public override function set height(value:Number):void
		{
			_height = value;
			redraw();
		}
		
		private function onEnterFrame(event:Event):void{
			var target:Number=open?0:_height;
			if(target!=_content.y){
				_content.y-=(_content.y-target)*0.5;
				if(Math.abs(_content.y-target)<5)
					_content.y=target
			}
		}
		
		private function redraw():void{
			_mask.graphics.clear();
			_mask.graphics.beginFill(0);
			_mask.graphics.drawRect(0,0,_width,_height);
			_content.graphics.clear();
			_content.graphics.beginFill(0xA7A9AC);
			_content.graphics.drawRoundRectComplex(0,0,_width,_height,10,10,0,0);
			for (var l:int=0; l<_layouts.length; l++){
				for (var j:int=0; j<_keys[l].length; j++){
					var row_width:Number=0;
					for (var i:int=0; i<_keys[l][j].length; i++){
						_keys[l][j][i].width=(_width)/_nx-10;
						row_width+=_keys[l][j][i].width+10;
						_keys[l][j][i].y=_height*j/_keys[0].length+5;
						_keys[l][j][i].height=(_height-30)/_keys[0].length;
					}
					var x0:Number=(_width-row_width)/2;
					for (i=0; i<_keys[l][j].length; i++){
						_keys[l][j][i].x=x0+5;
						x0+=_keys[l][j][i].width+10;
					}
				}
			}
			_openBtn.x=_width-_openBtn.width-20;
		}
	}
}